/**
 * 一次加载一个，滑动滑出效果页面
 */
const app = getApp();
Page({
  data: {
    startX: 0,
    endX: 0,
    dbName:"words",
    wordCount:null,
    wordLevel:null,
    wordIndex:0,
    word:null,
    wordList:null
  },

  onLoad: function (options) {
    let that = this;
    let type = options.type;
    //查询的数据库名称
    if (type == "phrase")
    {
      this.data.dbName = "word_phrase";
    }
    if (type == "root") {
      this.data.dbName = "word_root";
    }
    let count = parseInt(options.count);
    let level = parseInt(options.level);
    this.setData({
      wordCount: count,
      wordLevel: level
    });

    let filter = {};
    if ("word_root"==this.data.dbName)
    {
      //词根不分级别分类型
      filter.type=level;
    }else
    {
      filter.level = level;
    }
    this.data.filter = filter;

    //查询缓存，看原来已经看到哪里了

    let wordIndex = wx.getStorageSync(this.data.dbName + "_" + this.data.wordLevel);
    //如果缓存的不为空
    if (null != wordIndex)
    {
      this.setData({
        wordIndex: wordIndex
      });
    }
    this.loadWord("next");
  },
  
  //移动手指
  moveStart: function (e) {
    var startX = e.changedTouches[0].pageX;
    this.setData({
      startX: startX
    });
  },
  
  //滑动
  moveItem: function (e) {
    var that = this;
    var endX = e.changedTouches[0].pageX;
    this.setData({
      endX: endX
    });

    //计算手指触摸偏移剧距离
    var moveX = this.data.startX - this.data.endX;

    //向左移动
    if (moveX > 20) {
      this.next();
    }
    //向右移动
    if (moveX < -20) {
      this.previous();
    }
  },

  //从数据库加载单词 0指，本
  loadWord: function(flag) {
    let that =this;
    
    let wordIndex = this.data.wordIndex ;
    
    //下一个，且正好是逢10.是一页的开始
    let next10 = wordIndex % 10 == 0 && "next" == flag;
    //上一个，且正好是逢10.是一页的开始
    let pre10 = wordIndex % 10 == 9 && "previous" == flag;

    //正好是下一组，或者是上一组，或者，刚进来单词缓存还为空
    if (next10 || pre10 || null == this.data.wordList)
    {
      wx.showLoading();
      //调用分页云函数
      wx.cloud.callFunction({
        name: 'pagination',
        data: {
          dbName: this.data.dbName,
          pageIndex: Math.floor(wordIndex/10.0)+1,
          pageSize: 10,
          filter: this.data.filter
        },
        success(res) {
          wx.hideLoading();
          var word = res.result.data[wordIndex % 10];
          that.setData({
            word: word,
            wordList: res.result.data
          });
        },
        fail() {
        }
      });

      return;
    }

    that.setData({
      word: this.data.wordList[wordIndex % 10]
    });

    //缓存已经看到的位置
    wx.setStorageSync(this.data.dbName + "_" + this.data.wordLevel, wordIndex);
    return;
  },

  //上一题
  previous: function () {
    if (0 == this.data.wordIndex)
    {
      wx.showToast({
        title: '前面没有单词了',
        icon: 'none'
      })
      return;
    }
    let wordIndex = this.data.wordIndex;
    wordIndex--;
    this.setData({
      wordIndex: wordIndex
    });
    this.loadWord("previous");
    app.slide(this, "right");
  },
  //下一题
  next: function () {

    if (this.data.wordIndex == this.data.wordCount-1) {
      wx.showToast({
        title: '后面没有单词了',
        icon: 'none'
      })
      return;
    }

    let wordIndex = this.data.wordIndex;
    wordIndex++;

    this.setData({
      wordIndex: wordIndex
    });
    this.loadWord("next");
    app.slide(this, "left");
  }
})